<?php

namespace App\Http\Controllers\Admin;

use App\DTOS\Admin\UserRoleDTO;
use App\DTOS\Admin\SilentDTO;
use App\DTOS\Admin\UserMemberDTO;
use App\Http\Controllers\Controller;
use App\Models\Game\Silent;
use App\Services\Game\SilentService;
use App\Services\Game\UserMemberService;
use App\Services\Game\UserRoleService;
use App\Utils\ResponseUtil;
use Illuminate\Http\Request;

class SilentController extends Controller
{
    private Request $request;
    private SilentService $silentService;
    private UserRoleService $userRoleService;
    private UserMemberService $userMemberService;

    /**
     * SilentController constructor.
     * @param Request $request
     * @param SilentService $silentService
     * @param UserRoleService $userRoleService
     * @param UserMemberService $userMemberService
     */
    public function __construct(Request $request, SilentService $silentService,UserRoleService $userRoleService, UserMemberService $userMemberService)
    {
        $this->request = $request;
        $this->silentService = $silentService;
        $this->userRoleService = $userRoleService;
        $this->userMemberService = $userMemberService;
    }

    public function paginate(){
        $gameArea = request()->get('gameArea',0);
        $searchData = request()->all();
        $dto = new SilentDTO($searchData);
        $username = data_get($searchData,'username');
        $roleName = data_get($searchData,'roleName');
        $dto->setServerId($gameArea);
        if ($username){
            $userMemberDTO = new UserMemberDTO(['page'=>1,'limit'=>99999]);
            $userMemberDTO->setUsername($username);
            $paginate = $this->userMemberService->paginate($userMemberDTO);
            $userMemberIds = collect($paginate->items())->pluck('id')->toArray();
            if (empty($userMemberIds)){
                return ResponseUtil::error("没有数据");
            }
            $dto->setMemIds($userMemberIds);
        }
        if ($roleName){
            $userRoleDTO = new UserRoleDTO(['page'=>1,'limit'=>99999]);
            $userRoleDTO->setNick($roleName);
            $paginate = $this->userRoleService->paginate($userRoleDTO);
            $userRoleIds = collect($paginate->items())->pluck('id')->toArray();
            if (empty($userRoleIds)){
                return ResponseUtil::error("没有数据");
            }
            $dto->setRoleIds($userRoleIds);
        }
        $paginate = $this->silentService->paginate($dto);
        $data = $paginate->items();
        $memIds = collect($data)->pluck('mem_id')->toArray();
        $roleIds = collect($data)->pluck('role_id')->toArray();
        $members = $this->userMemberService->getByIds($memIds,['id','username']);
        $userRoles = $this->userRoleService->getByIds($roleIds,['id','nick']);

        foreach($data as &$datum){
             $datum['member'] = array_only(collect($members)->where('id',$datum['mem_id'])->first(),['id','username']);
             $datum['userRole'] = array_only(collect($userRoles)->where('id',$datum['role_id'])->first(),['id','nick']);
        }
        $res = [];
        $res['code'] = 0;
        $res['count'] = $paginate->total();
        $res['data'] = $data;
        return ResponseUtil::json($res);
    }

    public function add(){ //添加禁言
        $input = $this->request->only(['serverId','comment','roleId','disableTime']);
        $dto = new SilentDTO($this->request->all());
        try{
            global $gameArea;
            $gameArea = data_get($input,'serverId');
            $result = getMemAndRoleByRoleId($dto->getRoleId());
            $userRole = data_get($result,'userRole');
            $dto->setMemId($userRole['memid']);
            $dto->setRoleId($userRole['id']);
            $dto->setDisableTime(data_get($input,'disableTime'));
            $this->silentService->add($dto);
            return ResponseUtil::success();
        } catch (\Exception $e){
            return ResponseUtil::error($e->getMessage());
        }
    }

    public function detail($id)
    {
        $detail = Silent::query()->find($id);
        return ResponseUtil::success($detail);
    }

    public function edit($id){ //编辑禁言
        try{
            $disableTime = (int)request()->post('disableTime');
            $dto = new SilentDTO(['id'=>$id,'comment'=>request()->get('comment')]);
            $dto->setDisableTime($disableTime);

            $this->silentService->edit($dto);
            return ResponseUtil::success();
        } catch (\Exception $e){
            return ResponseUtil::error($e->getMessage());
        }
    }

}
